package com.college.controller.user;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.college.common.constant.ErrorMsg;
import com.college.common.pojo.ao.User;
import com.college.common.pojo.vo.ResultVo;
import com.college.common.utils.UserUtils;
import com.college.userInfo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;


@RestController
@RequestMapping("/user")
@Api(tags = "用户信息")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "注册账号")
    @PostMapping("sign-in")
    public ResultVo signIn(@RequestBody User user) {
        user.setSignInTime(new Timestamp(System.currentTimeMillis()));
        if (user.getAvatar() == null || "".equals(user.getAvatar())) {
            user.setAvatar("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        }
        if (userService.userSignIn(user)) {
            return ResultVo.success(user);
        }
        return ResultVo.fail(ErrorMsg.REGISTER_ERROR);
    }

    @ApiOperation(value = "登录")
    @RequestMapping("login")
    public ResultVo login(@RequestParam("accountNumber") @NotEmpty(message = "请输入账号") String accountNumber,
                          @RequestParam("userPassword") @NotEmpty(message = "请输入密码") String userPassword,
                          HttpServletResponse response) {
        User user = userService.userLogin(accountNumber, userPassword);

        if (ObjectUtils.isEmpty(user)) {
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }

        // 手机号长度不足11位
        if (user.getAccountNumber().length() != 11) {
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }

        if (ObjectUtils.isNotEmpty(user.getUserStatus()) && user.getUserStatus().equals((byte) 1)) {
            return ResultVo.fail(ErrorMsg.ACCOUNT_Ban);
        }
        return ResultVo.success(user);
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping("logout")
    public ResultVo logout() {
        Long shUserId = UserUtils.getHeaderUserId();
        return ResultVo.success();
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("info")
    public ResultVo getOneUser() {
        Long shUserId = UserUtils.getHeaderUserId();
        return ResultVo.success(userService.getUser(shUserId));
    }

    @ApiOperation(value = "修改用户公开信息")
    @PostMapping("/info")
    public ResultVo updateUserPublicInfo(@RequestBody User user) {
        Long shUserId = UserUtils.getHeaderUserId();
        user.setId(shUserId);
        if (userService.updateUserInfo(user)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "修改密码")
    @GetMapping("/password")
    public ResultVo updateUserPassword(@RequestParam("oldPassword") @NotEmpty(message = "请输入新的账号")String oldPassword,
                                       @RequestParam("newPassword") @NotEmpty(message = "请输入新的密码")String newPassword) {
        Long shUserId = UserUtils.getHeaderUserId();
        if (userService.updatePassword(newPassword, oldPassword, shUserId)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.PASSWORD_RESET_ERROR);
    }
}
